//
//  MTLuckyReportCell.swift
//  MT
//
//  Created by ooops on 2020/1/22.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

class MTLuckyReportCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    lazy var dataLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCRegularFont(14)
        l.textColor = UIColor.text.x1d1a1a
        return l
    }()
    
    lazy var detailLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCRegularFont(14)
        return l
    }()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.text.x999999
        return v
    }()
    
    class func reuseIdentity() -> String {
        return "MTLuckyReportCellID"
    }
    
    func bind(model: MTLuckyReportSubModel) {
        
        if let date = model.createTime {
            if let value = DateManager.date(dateString: date, format: "yyyy-MM-dd HH:mm:ss") {
                let dateString = DateManager.string(date: value, format: "yyyy-MM-dd HH:mm")
                //dataLabel.attributedText = arrtibute(text: "  \(dateString)", icon: "icon_time", position: 0)
                dataLabel.text = dateString
            }
        }
        
        if let ticketAmount = model.ticketAmount {
            
            let paraStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
            paraStyle.alignment = .right
            
            let attributeAll = [NSAttributedString.Key.font: PingFangSCRegularFont(14),
                                        NSAttributedString.Key.foregroundColor: UIColor.text.x1d1a1a,
                                        NSAttributedString.Key.paragraphStyle : paraStyle]
            
            let arrtibuteString: NSMutableAttributedString = NSMutableAttributedString(string: "获得\(ticketAmount)点券", attributes: attributeAll)
            
            let attribute = [NSAttributedString.Key.font: PingFangSCRegularFont(14),
                             NSAttributedString.Key.foregroundColor: UIColor(hex: 0xFFC000),
                             NSAttributedString.Key.paragraphStyle : paraStyle]
            
            
            arrtibuteString.addAttributes(attribute, range: "获得\(ticketAmount)点券".nsrange(of: "\(ticketAmount)")[0])
            
            detailLabel.attributedText = arrtibuteString
            
        }
        
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        makeConstraint()
        contentView.backgroundColor = UIColor.theme.background
        backgroundColor = UIColor.theme.background
    }
    
    private func setupUI() {
        contentView.addSubview(dataLabel)
        contentView.addSubview(detailLabel)
        contentView.addSubview(lineView)
    }
    
    private func makeConstraint() {
        dataLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(16)
            make.height.equalTo(20)
            make.width.equalTo(140)
        }
        
        detailLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-16)
            make.height.equalTo(20)
            make.left.equalTo(dataLabel.snp.right)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

}
